new Vue({
    el: '#openmain',
    data: () => ({
        sellinkfun: 0,
        linkval: '',
        linkarr: [],
        linkfun: [],//联系方式
        name: '',
        is_main: '0',
        position: '',
        sex: 1,
        iftijiao: true,
    }),
    methods: {
        editfun(index){
            this.linkval = this.linkarr[index].content
            if(this.linkarr[index].linkname){
                this.sellinkfun = this.linkarr[index].coattr_id
            }else {
                this.sellinkfun = 0
            }
            this.linkarr.splice(index,1)
        },
        addlink(){
            if (this.linkval.length && this.sellinkfun) {
                this.linkarr.push({
                    content: this.linkval,
                    linkname: $("#sellinkfun").find("option:selected").text(),
                    coattr_id: this.sellinkfun
                })
                this.linkval = ''
                this.sellinkfun = 0
            }
        },
        delitem(index){
            this.linkarr.splice(index, 1)
        },
        savefun(){
            if (this.iftijiao) {
                var data = {
                    linkmethods: this.linkarr,
                    sex: this.sex,
                    position: this.position,
                    co_id: $('#company_id').val(),
                    is_main: this.is_main ? '1' : '0',
                    linkid: $('#linkid').val(),
                    name: this.name,
                    act: $('#liaison').val()
                }
                $.ajax({
                    url: '/admin/customer/' + $('#liaison').val(),
                    type: 'post',
                    data,
                    success: (data) => {
                        data = JSON.parse(data)
                        if (data.status != 1) {
                            change_mes_type('.modal-body .alert',data.status)
                            $('.modal-body .alert').show()
                            $('.modal-body .alert p').html('*'+data.msg+'<br>')
                            //toastr.warning(data.msg)
                        } else {
                            change_mes_type('.alert',data.status)
                            this.iftijiao = false
                            $('.alert').show()
                            $('.alert p').html('*'+data.msg+'<br>')
                            //toastr.info(data.msg)
                            
                            $('#linkman_Modal').modal('hide');
                        }
                    }
                })
            }

        }
    },
    mounted(){
        //获取对应国家的联系方式
        $.ajax({
            url: '/admin/customer/' + $('#liaison').val(),
            data: {
                country_id: $('#country_id').val(),
                linkid: $('#linkid').val(),
                getliaison: ''
            },
            type: 'post',
            success: (data) => {
                data = JSON.parse(data)
                this.linkfun = data.getmethods
                if ($('#liaison').val() == 'liaisonedit') {
                    console.log(data)
                    this.name = data.info.name
                    this.is_main = data.info.is_main
                    this.position = data.info.position
                    this.sex = data.info.sex
                    this.linkarr = data.info.linkmethods
                }

            }
        })
    }
})
